package com.bookstore.services.Impl;


import com.bookstore.pojo.Cart;
import com.bookstore.pojo.Order;
import com.bookstore.pojo.OrderItem;
import com.bookstore.pojo.User;
import com.bookstore.services.BookService;
import com.bookstore.services.OrderItemService;
import com.bookstore.services.OrderService;

import java.util.List;

public class OrderServiceImpl implements OrderService {
    OrderItemService orderItemService=new OrderItemServiceImpl();
    BookService bookService=new BookServiceImpl();
    @Override
    public String checkout(Cart cart, User user) {
//        //封装订单对象
//        Order order = new Order();
//        order.setCreateDate(new Date());
//            //orderId运用算法生成
//        long millis = System.currentTimeMillis();//生成时间戳
//        String orderId=millis+""+user.getId();//订单号
//        order.setOrderId(orderId);
//        order.setTotalPrince(cart.getAmount());
//        order.setStatus(0);
//        order.setUserId(user.getId());
//        //封装订单项对象
//        List<CartItem> allItems = cart.getAllItems();
//        List<OrderItem> orderItems=new ArrayList<>();
//        for (CartItem cartItem:allItems) {
//            OrderItem orderItem=new OrderItem(cartItem.getBook().getName()
//                    ,cartItem.getCount(),
//                    cartItem.getTotalPrice(),
//                    cartItem.getBook().getPrice(),
//                    orderId);
//            orderItems.add(orderItem);
//        }
//        //保存订单
//        orderDao.saveOrder(order);
//        //保存订单项
//        orderItemService.saveItem(orderItems);
//        //修改相应库存,图书库存信息
//        for (CartItem cartItem:allItems) {
//            //获取详细信息,将从页面获取的图书从数据库查询并跟新数据内容
//            Book book = cartItem.getBook();
//            Book one = bookService.getOne(book);
//            Integer count = cartItem.getCount();
//            book.setStock(one.getStock()-count);
//            book.setSales(one.getSales()+count);
//            bookService.update(one);
//        }
//        return orderId;
        return null;
    }

    @Override
    public void updateStatus(String orderId, String status) {
//        Order order=new Order();
//        order.setOrderId(orderId);
//        int status1 = 0;
//        try {
//            status1 = Integer.parseInt(status);
//        } catch (NumberFormatException e) {
//            e.printStackTrace();
//        }
//        order.setStatus(status1);
//        orderDao.updateStatus(order);
    }

    @Override
    public List<Order> getAllOrder() {
        return null;
    }

    @Override
    public List<Order> getMyOrders(Integer userId) {
        return null;
    }

    @Override
    public OrderItem getMyOrder(String orderId) {
//        OrderItem orderItemOne = orderItemDao.getOrderItemOne(orderId);
        return null;
    }
}
